const express =require('express')
const router =express.Router()
module.exports =router

const db = require('../db')
const {formatDate} = require('../utils/utils')

router.get('/goodslist',async (req,res)=>{
    let sql = `select * from goods limit 0,10`;
    const data = await mysql.query(sql)
    const html =`
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <div class="box">
        <ul>
            ${
                data.map(item=>{
                    let str = ''
                    if(item.type=='select'){
                        str = `
                            <select name="${item.name}">
                            ${item.options.map((el)=>{
                                return `<option value="${el}">${el}</option>`
                            }).join('')}
                            </select>`    
                    }else if(item.type=='checkbox'){
                        str = `
                            ${item.options.map((el)=>{
                                return `<input type="${item.type}" name="${item.name}" value="${el}">${el}`
                             }).join('')}`
                    }else if(item.type=='radio'){
                        str = `
                            ${item.options.map((el)=>{
                                return `<input type="${item.type}" name="${item.name}" value="${el}">${el}`
                            }).join('')} `
                    } else {
                        str = `
                            <input type="${item.type}" name="${item.name}"> `
                    }
                    return  `<li>
                            <label class="l-name" >${item.label}:&nbsp</label>
                            ${str}
                        </li>`
                }).join('')
            }
            </ul>
        </div>
    </body>
    </html>
    `
    res.send(html)

})